package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Objects;

import com.ruoyi.common.core.domain.TreeLazySelect;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.DicTreeSelect;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysTreeDictData;
import com.ruoyi.system.service.ISysTreeDictDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 字典数据Controller
 * 
 * @author genghz
 * @date 2021-11-17
 */
@RestController
@RequestMapping("/system/tree/data")
public class SysTreeDictDataController extends BaseController
{
    @Autowired
    private ISysTreeDictDataService sysTreeDictDataService;

    /**
     * 查询字典数据列表
     */
    @PreAuthorize("@ss.hasPermi('system:tree/data:list')")
    @GetMapping("/list")
    public AjaxResult list(SysTreeDictData sysTreeDictData)
    {
        if(StringUtils.isEmpty(sysTreeDictData.getDictLabel())) {
            if(Objects.isNull(sysTreeDictData.getParentId())) {
                sysTreeDictData.setParentId(0L);
            }
        } else {
            sysTreeDictData.setParentId(null);
        }
        List<SysTreeDictData> list = sysTreeDictDataService.selectSysTreeDictDataList(sysTreeDictData);
        return AjaxResult.success(list);
    }

    @GetMapping("/treeselect")
    public AjaxResult treeselect(SysTreeDictData sysTreeDictData)
    {
        if(Objects.isNull(sysTreeDictData.getParentId())) {
            sysTreeDictData.setParentId(0L);
        }
        List<SysTreeDictData> list = sysTreeDictDataService.selectSysTreeDictDataList(sysTreeDictData);
        List<TreeLazySelect> treeSelects = sysTreeDictDataService.buildTreeSelect(list);
        return AjaxResult.success(treeSelects);
    }

    @GetMapping(value = "/getDictTree/{dictType}")
    public AjaxResult getSystemTree(@PathVariable("dictType") String dictType)
    {
        List<DicTreeSelect> list = sysTreeDictDataService.getSystemTreeData(dictType) ;
        return AjaxResult.success(list);
    }

    /**
     * 导出字典数据列表
     */
    @PreAuthorize("@ss.hasPermi('system:tree/data:export')")
    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysTreeDictData sysTreeDictData)
    {
        List<SysTreeDictData> list = sysTreeDictDataService.selectSysTreeDictDataList(sysTreeDictData);
        ExcelUtil<SysTreeDictData> util = new ExcelUtil<SysTreeDictData>(SysTreeDictData.class);
        return util.exportExcel(list, "字典数据数据");
    }

    /**
     * 获取字典数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:tree/data:query')")
    @GetMapping(value = "/{dataId}")
    public AjaxResult getInfo(@PathVariable("dataId") Long dataId)
    {
        return AjaxResult.success(sysTreeDictDataService.selectSysTreeDictDataByDataId(dataId));
    }

    /**
     * 新增字典数据
     */
    @PreAuthorize("@ss.hasPermi('system:tree/data:add')")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysTreeDictData sysTreeDictData)
    {
        return toAjax(sysTreeDictDataService.insertSysTreeDictData(sysTreeDictData));
    }

    /**
     * 修改字典数据
     */
    @PreAuthorize("@ss.hasPermi('system:tree/data:edit')")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysTreeDictData sysTreeDictData)
    {
        return toAjax(sysTreeDictDataService.updateSysTreeDictData(sysTreeDictData));
    }

    /**
     * 删除字典数据
     */
    @PreAuthorize("@ss.hasPermi('system:tree/data:remove')")
    @Log(title = "字典数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dataIds}")
    public AjaxResult remove(@PathVariable Long[] dataIds)
    {
        return toAjax(sysTreeDictDataService.deleteSysTreeDictDataByDataIds(dataIds));
    }


}
